package com.ruicar.afs.cloud.account.common.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruicar.afs.cloud.account.common.entity.VoucherBuryingPointConfiguration;
import com.ruicar.afs.cloud.account.common.entity.VoucherTemplateEvents;
import com.ruicar.afs.cloud.account.common.mapper.VoucherBuryingPointConfigurationMapper;
import com.ruicar.afs.cloud.account.common.service.VoucherBuryingPointConfigurationService;
import com.ruicar.afs.cloud.account.common.service.VoucherTemplateEventsService;
import com.ruicar.afs.cloud.common.core.query.QueryCondition;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

/**
 * @author infinity
 * @version 1.0
 * @date 2020/7/12 20:10
 */
@Service
@AllArgsConstructor
public class VoucherBuryingPointConfigurationServiceImpl extends ServiceImpl<VoucherBuryingPointConfigurationMapper, VoucherBuryingPointConfiguration> implements VoucherBuryingPointConfigurationService {
    private final VoucherTemplateEventsService voucherTemplateEventsService;

    @Override
    public Boolean addVoucherBuryingPoint(VoucherBuryingPointConfiguration voucherBuryingPointConfiguration) {
        check(voucherBuryingPointConfiguration);
        return this.save(voucherBuryingPointConfiguration);
    }

    @Override
    public Boolean delVoucherBuryingPoint(String id) {
        VoucherBuryingPointConfiguration voucherBuryingPointConfiguration = this.getById(id);
        Assert.isTrue(voucherBuryingPointConfiguration != null, "删除失败，凭证埋点事件配置不存在!");
        long count = voucherTemplateEventsService.count(Wrappers.<VoucherTemplateEvents>lambdaQuery()
                .eq(VoucherTemplateEvents::getBuriedPointNo, voucherBuryingPointConfiguration.getBuriedPointNo()));
        Assert.isTrue(count <= 0, "埋点事件编号[" + voucherBuryingPointConfiguration.getBuriedPointNo() + "]已被使用不可删除！");
        return this.removeById(id);
    }

    @Override
    public Boolean modifyVoucherBuryingPoint(VoucherBuryingPointConfiguration voucherBuryingPointConfiguration) {
        check(voucherBuryingPointConfiguration);
        VoucherBuryingPointConfiguration vonfiguration = this.getById(voucherBuryingPointConfiguration.getId());
        long count = voucherTemplateEventsService.count(Wrappers.<VoucherTemplateEvents>lambdaQuery()
                .eq(VoucherTemplateEvents::getBuriedPointNo, vonfiguration.getBuriedPointNo()));
        Assert.isTrue(count <= 0, "埋点事件编号[" + vonfiguration.getBuriedPointNo() + "]已被使用不可修改！");
        return this.updateById(voucherBuryingPointConfiguration);
    }

    @Override
    public IResponse query(QueryCondition<VoucherBuryingPointConfiguration> queryCondition) {
        IPage iPage = this.page(new Page<>(queryCondition.getPageNumber(), queryCondition.getPageSize()),
                Wrappers.<VoucherBuryingPointConfiguration>lambdaQuery()
                        .like(StringUtils.isNotEmpty(queryCondition.getCondition().getBuriedPointNo()), VoucherBuryingPointConfiguration::getBuriedPointNo, queryCondition.getCondition().getBuriedPointNo())
                        .like(StringUtils.isNotEmpty(queryCondition.getCondition().getBuriedPointName()), VoucherBuryingPointConfiguration::getBuriedPointName, queryCondition.getCondition().getBuriedPointName())
                        .like(StringUtils.isNotEmpty(queryCondition.getCondition().getBuriedPointDescribe()), VoucherBuryingPointConfiguration::getBuriedPointDescribe, queryCondition.getCondition().getBuriedPointDescribe())
        );
        return IResponse.success(iPage);
    }

    @Override
    public IResponse listBuryingPoint(VoucherBuryingPointConfiguration queryCondition) {
        return IResponse.success(this.list( Wrappers.<VoucherBuryingPointConfiguration>lambdaQuery().orderByDesc(VoucherBuryingPointConfiguration::getCreateTime)));
    }

    private void check(VoucherBuryingPointConfiguration voucherBuryingPointConfiguration) {
        long count = this.baseMapper.selectCount(Wrappers.<VoucherBuryingPointConfiguration>lambdaQuery()
                .eq(VoucherBuryingPointConfiguration::getBuriedPointNo, voucherBuryingPointConfiguration.getBuriedPointNo()).ne(voucherBuryingPointConfiguration.getId() != null, VoucherBuryingPointConfiguration::getId, voucherBuryingPointConfiguration.getId()));
        Assert.isTrue(count < 1, "埋点编号 [" + voucherBuryingPointConfiguration.getBuriedPointNo() + "]重复");

    }
}
